#include "comm.h"
#include "appconfig.h"

void uint16_to_str(rt_uint16_t value,rt_uint8_t *str)
{
	str[0] = (value & 0xff00)>>8;
	str[1] = (value & 0x00ff);
}

void uint32_to_str(rt_uint32_t value,rt_uint8_t *str)
{
	str[0] = (value & 0xff000000)>>24;
	str[1] = (value & 0x00ff0000)>>16;
	str[2] = (value & 0x0000ff00)>>8;
	str[3] = (value & 0x000000ff);
}

rt_uint16_t str_to_uint16(rt_uint8_t *str)
{
	rt_uint16_t value = 0;

	value = str[0];
	value <<= 8;
	value |= str[1];

	return value;
}

rt_uint32_t str_to_uint32(rt_uint8_t *str)
{
	rt_uint32_t value = 0;

	value = str[0];
	value <<= 8;
	value |= str[1];
	value <<= 8;
	value |= str[2];
	value <<= 8;
	value |= str[3];

	return value;
}


void show_hex_string(rt_uint8_t *str,rt_uint16_t size)
{
#ifndef RemovDebugCode
	rt_uint16_t i;

	for(i=0;i<size;i++)
	{
		rt_kprintf("%X ",str[i]);
	}
	rt_kprintf("\n");
#endif
}

void show_int_string(rt_uint8_t *str,rt_uint16_t size)
{
#ifndef RemovDebugCode
	rt_uint16_t i;

	for(i=0;i<size;i++)
	{
		rt_kprintf("%d ",str[i]);
	}
	rt_kprintf("\n");
#endif
}

void sys_reboot(void)
{
	NVIC_SystemReset();
}


rt_uint16_t net_crc16(rt_uint8_t *buf, rt_uint16_t len)
{
		rt_uint8_t j;
		rt_uint16_t i;
		rt_uint16_t a=0xffff;
		rt_uint16_t b=0xa001;
		
		for(i=0; i<len; i++)
		{
			a ^= buf[i];
			for(j=0;j<8;j++)
			{
				rt_uint16_t last;
				last = a % 2;
				a >>= 1;
				if(last == 1)
				{
					a ^= b;
				}
			}
		}
    return a;
}



